load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")
load("//bazel:tachyon.bzl", "if_macos", "if_posix")
load(
    "//bazel:tachyon_cc.bzl",
    "tachyon_cc_library",
    "tachyon_cc_unittest",
    "tachyon_objc_library",
)
load(":time_buildflags.bzl", "ENABLE_MACH_ABSOLUTE_TIME_TICKS", "time_buildflag_header")

package(default_visibility = ["//visibility:public"])

bool_flag(
    name = ENABLE_MACH_ABSOLUTE_TIME_TICKS,
    build_setting_default = True,
)

time_buildflag_header(
    name = "time_buildflags",
    enable_mach_absolute_time_ticks = ":" + ENABLE_MACH_ABSOLUTE_TIME_TICKS,
)

tachyon_cc_library(
    name = "time_base",
    hdrs = ["time.h"],
    visibility = ["//visibility:private"],
    deps = [
        ":time_buildflags",
        "//tachyon/base:compiler_specific",
        "//tachyon/base:logging",
        "//tachyon/base/numerics:clamped_math",
        "@com_google_absl//absl/time",
        "@com_google_googletest//:gtest_prod",
    ],
)

tachyon_cc_library(
    name = "time",
    srcs = ["time.cc"] + select({
        "@platforms//os:macos": [],
        "@platforms//os:windows": [],
        "//conditions:default": [
            "time_now_posix.cc",
        ],
    }) + if_posix([
        "time_conversion_posix.cc",
    ]),
    deps = [
        ":time_base",
        "//tachyon/base/numerics:checked_math",
    ] + if_macos([
        ":time_mac",
    ]),
)

tachyon_objc_library(
    name = "time_mac",
    srcs = ["time_mac.mm"],
    deps = [
        ":time_base",
        "//tachyon/base/mac:scoped_cftyperef",
        "//tachyon/base/mac:scoped_mach_port",
    ],
)

tachyon_cc_library(
    name = "time_delta_flag",
    srcs = ["time_delta_flag.h"],
    deps = [
        ":time",
        "//tachyon/base/flag",
        "//tachyon/base/numerics:safe_conversions",
    ],
)

tachyon_cc_library(
    name = "time_interval",
    srcs = ["time_interval.cc"],
    hdrs = ["time_interval.h"],
    deps = [":time"],
)

tachyon_cc_library(
    name = "time_stamp",
    srcs = ["time_stamp.cc"],
    hdrs = ["time_stamp.h"],
    deps = [":time"],
)

tachyon_objc_library(
    name = "time_mac_unittests",
    testonly = True,
    srcs = ["time_mac_unittest.mm"],
    deps = [
        ":time",
        "@com_google_googletest//:gtest",
    ],
    alwayslink = True,
)

tachyon_cc_unittest(
    name = "time_unittests",
    srcs = [
        "time_delta_flag_unittest.cc",
        "time_interval_unittest.cc",
        "time_stamp_unittest.cc",
        "time_unittest.cc",
    ],
    deps = [
        ":time_delta_flag",
        ":time_interval",
        ":time_stamp",
        "//tachyon/base/threading:platform_thread",
    ] + if_macos([
        ":time_mac_unittests",
    ]),
)
